﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IcbcOpenDemo.Models;
using IcbcOpenDemo.Models.Tools;
using System.Net;

namespace IcbcOpenDemo
{
    public static class DemoQHisD
    {
        /// <summary>
        /// 查询历只明细接口
        /// </summary>
        /// <param name="accno">账号</param>
        /// <param name="beginDate">开始日期yyyymmdd</param>
        /// <param name="endDate">结束日期yyyymmdd</param>
        public static void QHisD(string accno,string beginDate,string endDate)
        {
            RequestQHISD qhisd = new RequestQHISD
            {
                login_id = Params.Login_ID,
                tran_date = SeqNo.TranDate,
                tran_time = SeqNo.TranTime,
                f_seq_no = SeqNo.FSeqNo,
                account_no = accno,
                begin_date = beginDate,
                end_date = endDate,
                min_amount = "0",
                max_amount = "999999999999999",
                next_tag = "",
                currency = "CNY",
                due_bill_no = ""
            };

            // 将API交给通用请求类处理
            RequestCommon common = new RequestCommon(Params.APP_ID, Params.APIServiceUrl, Params.PRIVATE_KEY, qhisd);


            string stmp = common.GetStrToSign();
            string postContent = common.PostContent();

            Console.WriteLine(string.Format("待签名数据：{0}", common.GetStrToSign()));

            Console.WriteLine(string.Format("待Post数据：{0}", common.PostContent()));

            //Thread.Sleep(1000);
            Console.WriteLine("ActionString:" + common.ActionString());

            //Console.WriteLine(string.Format("签名结果：{0}", common.sign));
            //Console.ReadKey();


            HttpWebRequest request = WebRequest.Create(common.ActionString()) as HttpWebRequest;


            request.ProtocolVersion = HttpVersion.Version10;
            request.ContentType = "application/x-www-form-urlencoded;charset=GBK";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            request.Method = "POST";
            request.Timeout = 8000;  //100000=1秒
            request.ReadWriteTimeout = 30000;
            //request.ServicePoint.Expect100Continue = false;  //必须

            Encoding encode = Encoding.GetEncoding("UTF-8");
            byte[] bytesToPost = encode.GetBytes(common.PostContent());

            using (System.IO.Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytesToPost, 0, bytesToPost.Length);
            }

            System.IO.Stream responseStream = request.GetResponse().GetResponseStream();

            string strResponse;
            using (System.IO.StreamReader responseReader = new System.IO.StreamReader(responseStream, encode))
            {
                strResponse = responseReader.ReadToEnd();
            }
            responseStream.Close();

            Console.WriteLine("返回结果：");
            Console.WriteLine(strResponse);
        }
    }
}
